﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sylvain.Manager;
using Sylvain.Model;


public partial class _Default : System.Web.UI.Page
{ 
    //Instantiation of SylvainManager Class.
    ISylvainManager objManager = new SylvainManager();

    /// <summary>
    /// Display Page Content.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             try
             {
                 int pageId = objManager.GetPageId("home");
                 string pageContent = objManager.GetPageContent(pageId);
                 if (pageContent.Length > 0)
                 {
                     spanDetails.InnerHtml = pageContent;
                     SelectedSearchText();
                 }
                 // Display Latest Description of Quote.
                 NewsSection objQuote = new NewsSection();
                 objQuote = objManager.GetLatestQuote((int)Tag.Quote);
                 if (objQuote.Description != null && objQuote.Description.Length > 0)
                 {
                     lblQuote.Text = objQuote.Description;

                 }
                 //else { imgQuote.Visible = false; }
                 //Display Latest Description of PressRelease.
                 NewsSection objPress = new NewsSection();
                 objPress = objManager.GetLatestPressRelease((int)Tag.PressRelease);
                 if (objPress.Description != null && objPress.Description.Length > 0)
                 {
                     lblPressRelease.Text = objPress.Description;

                 }
                 else { imgPressRelease.Visible = false; }
                 //Display Latest Description of News.
                 NewsSection objNews = new NewsSection();
                 objNews = objManager.GetLatestNews((int)Tag.News);
                 if (objNews.Description != null && objNews.Description.Length > 0)
                 {
                     lblNews.Text = objNews.Description;
                 }
                 else { imgNews.Visible = false; }
             }
             catch
             {
                 //lblError.Visible = true;
                 //lblError.Text = "Operation could not be performed.Please try again";
             }
         }
         else
         {
             SelectedSearchText();
             Session[AppSession.SearchText.ToString()] = (this.Page.Master as Menu).GetSearchText.Trim();
         }
    }
    /// <summary>
    /// Display Complete Description for News
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgNews_Click(object sender, ImageClickEventArgs e)
    {
        NewsSection obj = new NewsSection();
        obj = objManager.GetLatestNews((int)Tag.News);
        if (obj != null)
        {
            if (obj.Description != null && obj.Description.ToLower().StartsWith("<p"))
            {
                spanDetails.InnerHtml = "<h1>&nbsp;&nbsp;" + obj.Title + "</h1>" + obj.Description + "";
            }
            else
            {
                spanDetails.InnerHtml = "<h1>&nbsp;&nbsp;" + obj.Title + "</h1><p>" + obj.Description + "</p>";
            }
        }
        SelectedSearchText();
    }
    /// <summary>
    /// Display Complete Description for Press Release
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgPressRelease_Click(object sender, ImageClickEventArgs e)
    {
        NewsSection obj = new NewsSection();
        obj = objManager.GetLatestPressRelease((int)Tag.PressRelease);
        if (obj != null)
        {
            if (obj.Description != null && obj.Description.ToLower().StartsWith("<p"))
            {
                spanDetails.InnerHtml = "<h1>&nbsp;&nbsp;" + obj.Title + "</h1>" + obj.Description + "";
            }
            else
            {
                spanDetails.InnerHtml = "<h1>&nbsp;&nbsp;" + obj.Title + "</h1><p>" + obj.Description + "</p>";
            }
        }
        SelectedSearchText();
    }
    /// <summary>
    /// Display Complete Description for Quote of the Day
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgQuote_Click(object sender, ImageClickEventArgs e)
    {
            NewsSection obj = new NewsSection();
            obj = objManager.GetLatestQuote((int)Tag.Quote);
            if (obj != null)
            {
                spanDetails.InnerHtml = "<h1>" + obj.Title + "</h1><p>" + obj.Description + "</p>";
            }
            SelectedSearchText();
    }

    private void SelectedSearchText()
    {
        if((this.Page.Master as Menu).GetSearchText.Trim() != "")
        {
            Session[AppSession.SearchText.ToString()]=(this.Page.Master as Menu).GetSearchText.Trim();
        }
        else if(Session[AppSession.SearchText.ToString()] != null)
        {
            (this.Page.Master as Menu).GetSearchText = Session[AppSession.SearchText.ToString()].ToString();
        }
        if ((this.Page.Master as Menu).GetSearchText.Trim() != "")
        {
            spanDetails.InnerHtml = spanDetails.InnerHtml.Replace("HighlightSelected", "");
            spanDetails.InnerHtml = spanDetails.InnerHtml.Replace((this.Page.Master as Menu).GetSearchText, "<span class='HighlightSelected'>" + (this.Page.Master as Menu).GetSearchText + "</span>");
        }
    }
   
}
           